package com.jp.knowledge.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jiayen.util.DisplayUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.model.InfoData;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/10.
 */

public class InfoDetailToolsPopupWindow extends PopupWindow implements View.OnClickListener {

	private Context mContext;
	private LinearLayout contentView;

	public InfoDetailToolsPopupWindow(Context context){
		mContext = context;
		contentView = new LinearLayout(mContext);
		contentView.setGravity(Gravity.CENTER);
		contentView.setBackgroundResource(R.drawable.popup_download);
		contentView.setOrientation(LinearLayout.VERTICAL);
		contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setContentView(contentView);
		setOutsideTouchable(true);
		setFocusable(true);
		setBackgroundDrawable(new ColorDrawable(0x00000000));

		contentView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				contentView.getViewTreeObserver().removeOnPreDrawListener(this);
				dismiss();
				View parentView = (View) contentView.getTag();
				showAsDropDown(parentView, 0,
						-contentView.getMeasuredHeight()-parentView.getMeasuredHeight());
				return false;
			}
		});
	}

	public void setData(List<InfoData.AppListBean> appList){
		contentView.removeAllViews();
		if(appList == null || appList.size() == 0)
			return;
		for(int i = 0; i < appList.size(); i++){
			TextView textView = new TextView(mContext);
			textView.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
			textView.setSingleLine(true);
			textView.setGravity(Gravity.CENTER);
			textView.setTextColor(mContext.getResources().getColor(R.color.white_press));
			textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimension(R.dimen.min_font));
			textView.setText(appList.get(i).getAppName());
			textView.setTag(i);
			textView.setOnClickListener(this);
			contentView.addView(textView);
			if(i != appList.size()-1){
				View line = new View(mContext);
				line.setBackgroundResource(R.color.white_press);
				LinearLayout.LayoutParams lineLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
						(int) (mContext.getResources().getDimension(R.dimen.line_heigth)+0.5));
				lineLayoutParams.topMargin = (int) mContext.getResources().getDimension(R.dimen.space_middle);
				lineLayoutParams.bottomMargin = (int) mContext.getResources().getDimension(R.dimen.space_middle);
				line.setLayoutParams(lineLayoutParams);
				contentView.addView(line);
			}
		}
	}

	public void show(View parentView){
		contentView.setTag(parentView);
		contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		setWidth(parentView.getMeasuredWidth());
		showAsDropDown(parentView, 0,
				-contentView.getMeasuredHeight()-parentView.getMeasuredHeight());
	}

	@Override
	public void onClick(View v) {
		int position = (int) v.getTag();
		if(this.toolsItemClickListener != null)
			this.toolsItemClickListener.onToolsItemClick(position);
		dismiss();
	}

	private ToolsItemClickListener toolsItemClickListener;
	public void setToolsItemClickListener(ToolsItemClickListener toolsItemClickListener){
		this.toolsItemClickListener = toolsItemClickListener;
	}
	public interface ToolsItemClickListener{
		void onToolsItemClick(int position);
	}
}
